package cz.vutbr.fit.xzemko01.tfmcbs.validation.validators;

import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;

import cz.vutbr.fit.xzemko01.tfmcbs.model.Component;
import cz.vutbr.fit.xzemko01.tfmcbs.model.CompositeComponent;
import cz.vutbr.fit.xzemko01.tfmcbs.model.Port;
import cz.vutbr.fit.xzemko01.tfmcbs.validation.utils.DiagnosticBuilder;

public final class CompositeComponentValidator implements Validator<CompositeComponent> {
	
	private static CompositeComponentValidator compositeComponentValidator;
	
	private Validator<Component> componentValidator;
	
	private CompositeComponentValidator() {
		componentValidator = ComponentValidator.getInstance();
	}
	
	public static Validator<CompositeComponent> getInstance() {
		if(compositeComponentValidator == null) {
			compositeComponentValidator = new CompositeComponentValidator();
		}
		return compositeComponentValidator;
	}
	
	@Override
	public boolean validate(CompositeComponent component, DiagnosticChain diagnostics, Map<Object, Object> context) {
		boolean result = componentValidator.validate(component, diagnostics, context);
		result &= validateComponentHasPorts(component, diagnostics, context);
		return result;
	}
	
	private boolean validateComponentHasPorts(CompositeComponent component, DiagnosticChain diagnostics, Map<Object, Object> context) {
		List<Port> portList = component.getPorts();
		if(portList == null || portList.isEmpty()) {
			Diagnostic diagnostic = new DiagnosticBuilder(component)
				.setMessage("Composite component \"" + component.getName() + "\" has no ports.")
				.setSeverity(Diagnostic.WARNING).build();
			diagnostics.add(diagnostic);
			return false;
		}
		return true;
	}
}
